package com.feihong.txgw.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.feihong.txgw.R;
import com.feihong.txgw.app.App;

/**
 * @author wangziheng
 * @name txgw_app
 * @class name：com.feihong.txgw.utils
 * @class describe: 自定义Toast
 * @time 2017/8/30 12:40
 * @change
 * @chang time
 * @class describe
 */
public class ToastUtils {
    /**
     * 添加进购物车专用toast
     */
    private static Toast mShopCarToast;
    /**
     * 普通short toast
     */
    private static Toast mShortToast;
    /**
     * 普通toast  view
     */
    private static View mAutoNormalView;

    /**
     * msg显示的textview
     * textview的背景layout
     */
    TextView mMsgTextView;

    /**
     * 普通long  toast
     */
    private static Toast mLongToast;
    private ToastUtils() {
    }

    public static ToastUtils getInstance() {
        return new ToastUtils();
    }

    /**
     * 添加商品到购物车  成功的  吐司效果
     *
     * @param context
     */
    public static void showAddShopCarSuccessToast(Context context) {
        View addShopCarSuccessView;
        if (mShopCarToast == null) {
            mShopCarToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
        }
        mShopCarToast.setGravity(Gravity.CENTER, 0, 0);
        addShopCarSuccessView = LayoutInflater.from(context).inflate(R.layout.add_shopcar_success_icon_layout, null, false);
        mShopCarToast.setView(addShopCarSuccessView);
        mShopCarToast.show();
    }

    /**
     * 普通的toast样式
     *
     * @param msg
     */
    public void showShortToast(String msg) {
        showShortToastAutoColor(msg,  0);
    }

    public void showShortToastAutoColor(String msg,int textColor) {
        if(mShortToast == null){
            mShortToast = Toast.makeText(App.getApp().getApplication(), msg, Toast.LENGTH_SHORT);
        }
        mShortToast.setGravity(Gravity.BOTTOM, 0, 0);
        if (mAutoNormalView == null) {
            mAutoNormalView = LayoutInflater.from(App.getApp().getApplication()).inflate(R.layout.toast_autocolor_normal_layout, null, false);
        }
        mMsgTextView = (TextView) mAutoNormalView.findViewById(R.id.toast_normal_auto_textView);
        if (!TextUtils.isEmpty(msg)) {
            mMsgTextView.setText(msg);
        } else {
            throw new NullPointerException("请输入toast内容");
        }
        mMsgTextView.setTextColor(textColor != 0 ? textColor : App.getApp().getApplication().getResources().getColor(R.color.white));
        mShortToast.setView(mAutoNormalView);
        mShortToast.show();
    }

    public void showLongToast(String msg){
        showLongToastAutoColor(msg ,0);
    }
    public void showLongToastAutoColor(String msg,int textColor){
        if(mLongToast == null){
            mLongToast = Toast.makeText(App.getApp().getApplication(), msg, Toast.LENGTH_LONG);
        }
        mLongToast.setGravity(Gravity.BOTTOM, 0, 0);
        if (mAutoNormalView == null) {
            mAutoNormalView = LayoutInflater.from(App.getApp().getApplication()).inflate(R.layout.toast_autocolor_normal_layout, null, false);
        }
        mMsgTextView = (TextView) mAutoNormalView.findViewById(R.id.toast_normal_auto_textView);
        if (!TextUtils.isEmpty(msg)) {
            mMsgTextView.setText(msg);
        } else {
            throw new NullPointerException("请输入toast内容");
        }
        mMsgTextView.setTextColor(textColor != 0 ? textColor : App.getApp().getApplication().getResources().getColor(R.color.white));
        mLongToast.setView(mAutoNormalView);
        mLongToast.show();
    }
    public static synchronized void cannelToast(){
        if(mShortToast != null){
            mShortToast.cancel();
            mShortToast = null;
        }
        if(mLongToast != null){
            mLongToast.cancel();
            mLongToast = null;
        }
    }
}
